package cn.supermaster.management.web.service.system.impl;

import cn.supermaster.management.common.constant.UserConstant;
import cn.supermaster.management.common.domain.SystemRole;
import cn.supermaster.management.common.domain.SystemUser;
import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.common.utils.ShiroUtils;
import cn.supermaster.management.common.utils.WebHelper;
import cn.supermaster.management.common.utils.poi.ExcelUtil;
import cn.supermaster.management.core.util.AuthorizationUtils;
import cn.supermaster.management.system.domain.SystemUserRole;
import cn.supermaster.management.system.service.ISystemRoleService;
import cn.supermaster.management.system.service.ISystemUserService;
import cn.supermaster.management.web.service.system.ISRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.service.system.impl
 * @ClassName: SystemRoleServiceImpl
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/1/11 23:10
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.comn Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class SRoleServiceImpl implements ISRoleService {
    private String prefix = "system/role";

    @Autowired
    private ISystemRoleService roleService;

    @Autowired
    private ISystemUserService userService;

    /*
     * ================================================
     *  页面跳转功能实现
     * ================================================
     */

    /**
     * @Description: [跳转到role角色首页界面]
     * <p></P>
     * @Title: skipRoleIndexHtml
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param:
     * @Return: java.lang.String
     * @Throws:
     */
    @Override
    public String skipRoleIndexHtml() {
        return prefix + "/role";
    }

    /**
     * @Description: [跳转新增角色界面]
     * <p></P>
     * @Title: skipAddRoleHtml
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Return: java.lang.String
     */
    @Override
    public String skipAddRoleHtml() {
        return prefix + "/add";
    }

    /**
     * @Description: [跳转修改角色界面]
     * <p></P>
     * @Title: skipModifyRoleHtml
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: modelMap
     * @Param: roleId
     * @Return: java.lang.String
     */
    @Override
    public String skipModifyRoleHtml(ModelMap modelMap, Long roleId) {
        SystemRole role = roleService.selectRoleById(roleId);
        modelMap.put("role", role);
        return prefix + "/edit";
    }

    /**
     * @Description: [跳转角色分配数据权限界面]
     * <p></P>
     * @Title: skipAuthDataScopeHtml
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: modelMap
     * @Param: roleId
     * @Return: java.lang.String
     */
    @Override
    public String skipAuthDataScopeHtml(ModelMap modelMap, Long roleId) {
        modelMap.put("role", roleService.selectRoleById(roleId));
        return prefix + "/dataScope";
    }

    /**
     * @Description: [跳转选择菜单树界面]
     * <p></P>
     * @Title: skipSelectMenuTreeHtml
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Return: java.lang.String
     */
    @Override
    public String skipSelectMenuTreeHtml() {
        return prefix + "/tree";
    }

    /**
     * @Description: [跳转分配用户界面]
     * <p></P>
     * @Title: skipAuthUserHtml
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: modelMap
     * @Param: roleId
     * @Return: java.lang.String
     */
    @Override
    public String skipAuthUserHtml(ModelMap modelMap, Long roleId) {
        modelMap.put("role", roleService.selectRoleById(roleId));
        return prefix + "/authUser";
    }

    /**
     * @Description: [跳转选择用户界面]
     * <p></P>
     * @Title: skipSelectedUserHtml
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: modelMap
     * @Param: roleId
     * @Return: java.lang.String
     */
    @Override
    public String skipSelectedUserHtml(ModelMap modelMap, Long roleId) {
        modelMap.put("role", roleService.selectRoleById(roleId));
        return prefix + "/selectUser";
    }

    /*
     * ================================================
     *  Rest功能实现
     * ================================================
     */

    /**
     * @Description: [新增角色信息]
     * <p></P>
     * @Title: doAddRoleInformation
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: role
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doAddRoleInformation(SystemRole role) {
        if (UserConstant.ROLE_NAME_NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return WebHelper.error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstant.ROLE_KEY_NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            return WebHelper.error("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        role.setCreateBy(ShiroUtils.getuserName());
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return WebHelper.webResult(roleService.insertRole(role));
    }

    /**
     * @Description: [导出角色信息]
     * <p></P>
     * @Title: doExportRoleInformation
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: role
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doExportRoleInformation(SystemRole role) {
        List<SystemRole> list = roleService.selectRoleList(role);
        ExcelUtil<SystemRole> util = new ExcelUtil<SystemRole>(SystemRole.class);
        return util.exportExcel(list, "角色数据");
    }


    /**
     * @Description: [根据条件获取角色列表]
     * <p></P>
     * @Title: queryRoleListByCondition
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: role
     * @Return: cn.supermaster.management.common.page.TableDataInfo
     */
    @Override
    public TableDataInfo queryRoleListByCondition(SystemRole role) {
        WebHelper.startPage();
        List<SystemRole> list = roleService.selectRoleList(role);
        return WebHelper.getDataTable(list);
    }

    /**
     * @Description: [查询已分配用户角色列表]
     * <p></P>
     * @Title: queryAllocatedUserRoleList
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: user
     * @Return: cn.supermaster.management.common.page.TableDataInfo
     */
    @Override
    public TableDataInfo queryAllocatedUserRoleList(SystemUser user) {
        WebHelper.startPage();
        List<SystemUser> list = userService.selectAllocatedList(user);
        return WebHelper.getDataTable(list);
    }

    /**
     * @Description: [查询未分配用户角色列表]
     * <p></P>
     * @Title: queryUnallocatedUserRoleList
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: user
     * @Return: cn.supermaster.management.common.page.TableDataInfo
     */
    @Override
    public TableDataInfo queryUnallocatedUserRoleList(SystemUser user) {
        WebHelper.startPage();
        List<SystemUser> list = userService.selectUnallocatedList(user);
        return WebHelper.getDataTable(list);
    }

    /**
     * @Description: [取消授权]
     * <p></P>
     * @Title: doCancelUserAuthorization
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: userRole
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doCancelUserAuthorization(SystemUserRole userRole) {
        return WebHelper.webResult(roleService.deleteAuthUser(userRole));
    }

    /**
     * @Description: [批量选择用户授权]
     * <p></P>
     * @Title: queryUserAllAuthorizations
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: roleId
     * @Param: userIds
     * @Return: cn.supermaster.management.common.response.WebResult
     * @Throws:
     */
    @Override
    public WebResponse queryUserAllAuthorizations(Long roleId, String userIds) {
        return WebHelper.webResult(roleService.insertAuthUsers(roleId, userIds));
    }

    /**
     * @Description: [批量取消授权]
     * <p></P>
     * @Title: doCancelAllUserAuthorizations
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: roleId
     * @Param: userIds
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doCancelAllUserAuthorizations(Long roleId, String userIds) {
        return WebHelper.webResult(roleService.deleteAuthUsers(roleId, userIds));
    }

    /**
     * @Description: [修改角色信息]
     * <p></P>
     * @Title: doModifyRoleInformation
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: role
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doModifyRoleInformation(SystemRole role) {
        roleService.checkRoleAllowed(role);
        if (UserConstant.ROLE_NAME_NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role))) {
            return WebHelper.error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if (UserConstant.ROLE_KEY_NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role))) {
            return WebHelper.error("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        role.setUpdateBy(ShiroUtils.getuserName());
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return WebHelper.webResult(roleService.updateRole(role));
    }

    /**
     * @Description: [角色状态修改]
     * <p></P>
     * @Title: doModifyRoleStatus
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: role
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doModifyRoleStatus(SystemRole role) {
        roleService.checkRoleAllowed(role);
        return WebHelper.webResult(roleService.changeStatus(role));
    }

    /**
     * @Description: [保存角色分配数据权限]
     * <p></P>
     * @Title: doAddAuthDataScope
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: role
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doAddAuthDataScope(SystemRole role) {
        roleService.checkRoleAllowed(role);
        role.setUpdateBy(ShiroUtils.getuserName());
        if (roleService.authDataScope(role) > 0) {
            ShiroUtils.setSystemUser(userService.selectUserById(ShiroUtils.getSystemUser().getUserId()));
            return WebHelper.success();
        }
        return WebHelper.error();
    }

    /**
     * @Description: [删除角色信息]
     * <p></P>
     * @Title: doRemoveRoleInformation
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: ids
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse doRemoveRoleInformation(String ids) {
        return WebHelper.webResult(roleService.deleteRoleByIds(ids));
    }

    /**
     * @Description: [校验角色名称唯一性]
     * <p></P>
     * @Title: doCheckRoleNameUnique
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: role
     * @Return: java.lang.String
     */
    @Override
    public String doCheckRoleNameUnique(SystemRole role) {
        return roleService.checkRoleNameUnique(role);
    }

    /**
     * @Description: [校验角色权限Keys]
     * <p></P>
     * @Title: doCheckRoleKeyUnique
     * @Author: Lance Ting
     * @Date: 2021-01-11
     * @Param: role
     * @Return: java.lang.String
     */
    @Override
    public String doCheckRoleKeyUnique(SystemRole role) {
        return roleService.checkRoleKeyUnique(role);
    }

}